package uk.me.dewi.appenin.android.entity;

import java.util.Date;
import java.util.HashMap;

import uk.me.dewi.validation.ValidationException;

import com.google.android.maps.GeoPoint;

public class Place {
    
    public Long mId;
    public String mName;
    public String mDescription;
    public int mRatings;
    public String[] mTags;
    public Date mCreated;
    public Date mModified;
    public String mUser;
    public GeoPoint mGeo;
    
    public Place(){
        this.mCreated = new Date();
        this.mModified = mCreated;
    }
    
    public Place(String name, String description){
        this();
        this.mName = name;
        this.mDescription = description;
    }
    
    public String toString(){
        return "Place[id="+mId+", name="+mName+"]";
    }
    
    public Place validate() {
        HashMap<String, String> problems = new HashMap<String, String>();
        
        if(mName == null) problems.put("name", "null");
        else if(mName.trim().length() == 0) problems.put("name", "empty");
        
        if(mDescription == null) problems.put("description", "null");
        else if(mDescription.trim().length() == 0) problems.put("description", "empty");
        
        if(!problems.isEmpty()){
            throw new ValidationException("Invalid Place", problems);
        }
        
        return this;
    }
    
    public String getGeoString(){
        return new StringBuilder()
                        .append(mGeo.getLatitudeE6()/1E6)
                        .append(",")
                        .append(mGeo.getLongitudeE6()/1E6)
                        .toString();
    }

    public boolean isNew(){
        return mId == null;
    }
}
